  --
  -- Function: DELETE_EXT_FIELD_VALUES
  --
  -- Action: Delete all the extension values defined for an extended item.
  --
  -- In Parameters: Extension table and extended item id.
  --
  -- Out Parameters: None.
  --
  -- Return: NUMBER with the number of registries affected.
  --
  -- Exception: There is no catching mechanism for exceptions so if anyone
  -- is raised, it will be propagated.

  CREATE OR REPLACE FUNCTION "DELETE_EXT_FIELD_VALUES" (
	"ExtFieldStringValue"."idValue"%TYPE,
	"ExtTableDefinition"."codExtTable"%TYPE
  ) RETURNS INTEGER AS $$

  DECLARE ID_ITEM ALIAS FOR $1;
  DECLARE COD_EXT_TABLE ALIAS FOR $2;


  DECLARE v_qry VARCHAR(2000) := ' ';
          v_cont INTEGER := 0;
	  v_tmp INTEGER;

  BEGIN
	-- Build the delete query for values of string type extension fields
        v_qry := 'DELETE FROM "ExtFieldStringValue" WHERE "idValue"= ' || ID_ITEM || ' AND "idExtField" IN ' || 
		'(SELECT "idExtField" FROM "ExtFieldDefinition" f, "ExtTableDefinition" t WHERE t."idExtTable" = f."idExtTable" ' ||
		'AND t."codExtTable" = ' || quote_literal(COD_EXT_TABLE) || ');';

	-- Run the delete query for values of string type extension fields
	EXECUTE v_qry;

	-- Get the number of rows affected by the delete query
	GET DIAGNOSTICS v_tmp = ROW_COUNT;

	-- Add the number of affected rows to the total sum
	v_cont := v_cont + v_tmp;

	-- Build the delete query for values of numeric type extension fields
        v_qry := 'DELETE FROM "ExtFieldNumberValue" WHERE "idValue"= ' || ID_ITEM || ' AND "idExtField" IN ' || 
		'(SELECT "idExtField" FROM "ExtFieldDefinition" f, "ExtTableDefinition" t WHERE t."idExtTable" = f."idExtTable" ' ||
		'AND t."codExtTable" = ' || quote_literal(COD_EXT_TABLE) || ');';

	-- Run the delete query for values of numeric type extension fields
	EXECUTE v_qry;

	-- Get the number of rows affected by the delete query
	GET DIAGNOSTICS v_tmp = ROW_COUNT;

	-- Add the number of affected rows to the total sum
	v_cont := v_cont + v_tmp;

	-- Build the delete query for values of date type extension fields
        v_qry := 'DELETE FROM "ExtFieldDateValue" WHERE "idValue"= ' || ID_ITEM || ' AND "idExtField" IN ' || 
		'(SELECT "idExtField" FROM "ExtFieldDefinition" f, "ExtTableDefinition" t WHERE t."idExtTable" = f."idExtTable" ' ||
		'AND t."codExtTable" = ' || quote_literal(COD_EXT_TABLE) || ');';

	-- Run the delete query for values of date type extension fields
	EXECUTE v_qry;

	-- Get the number of rows affected by the delete query
	GET DIAGNOSTICS v_tmp = ROW_COUNT;

	-- Add the number of affected rows to the total sum
	v_cont := v_cont + v_tmp;

	-- Build the delete query for values of boolean type extension fields
        v_qry := 'DELETE FROM "ExtFieldBooleanValue" WHERE "idValue"= ' || ID_ITEM || ' AND "idExtField" IN ' || 
		'(SELECT "idExtField" FROM "ExtFieldDefinition" f, "ExtTableDefinition" t WHERE t."idExtTable" = f."idExtTable" ' ||
		'AND t."codExtTable" = ' || quote_literal(COD_EXT_TABLE) || ');';

	-- Run the delete query for values of boolean type extension fields
	EXECUTE v_qry;

	-- Get the number of rows affected by the delete query
	GET DIAGNOSTICS v_tmp = ROW_COUNT;

	-- Add the number of affected rows to the total sum
	v_cont := v_cont + v_tmp;

	-- Build the delete query for values of long type extension fields
        v_qry := 'DELETE FROM "ExtFieldLongValue" WHERE "idValue"= ' || ID_ITEM || ' AND "idExtField" IN ' || 
		'(SELECT "idExtField" FROM "ExtFieldDefinition" f, "ExtTableDefinition" t WHERE t."idExtTable" = f."idExtTable" ' ||
		'AND t."codExtTable" = ' || quote_literal(COD_EXT_TABLE) || ');';

	-- Run the delete query for values of long type extension fields
	EXECUTE v_qry;

	-- Get the number of rows affected by the delete query
	GET DIAGNOSTICS v_tmp = ROW_COUNT;

	-- Add the number of affected rows to the total sum
	v_cont := v_cont + v_tmp;


        RETURN v_cont;

  END;
  $$ LANGUAGE plpgsql